package com.wz.utils.list;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestCollection {
    public static void main(String[] args) {
//        deleteByIterator();
        delete1ByForeach();
//        delete2ByForeach();
    }

    public static void deleteByIterator() {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String item = iterator.next();
            if ("1".equals(item)) {
                iterator.remove();
            }
        }
        System.out.println(list);
    }

    // TODO 为啥不报错？
    public static void delete1ByForeach() {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        for (String item : list) {
            if ("1".equals(item)) {
                list.remove(item);
            }
        }
        System.out.println(list);
    }

    public static void delete2ByForeach() {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        for (String item : list) {
            if ("2".equals(item)) {
                list.remove(item);
            }
        }
        System.out.println(list);
    }
}
